package com.gss.security.persistence;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gss.armory.master.ApplicationUrlMappingDTO;
import com.gss.security.domain.RoleDetailsDTO;
import com.gss.security.domain.UserDetailsDTO;
import com.gss.security.domain.UserRoleMappingDTO;

public class AuthenticationDAO extends HibernateDaoSupport {
	
	private Logger logger = Logger.getLogger(AuthenticationDAO.class);
	
	public UserDetailsDTO getDetailsForUserId(String strUserId) throws Exception {
		logger.debug("inside authentication dao: method(getDetailsForUserId): ");
		
		UserDetailsDTO userDetailsDTO = new UserDetailsDTO();
		getHibernateTemplate().load(userDetailsDTO, strUserId);
		logger.debug("UserDetailsDTO Object for user id:" + strUserId + " is: " + userDetailsDTO.toString());
		return userDetailsDTO;
	}
	
	public void updatePasswordForUser(String updateQuery, UserDetailsDTO userDetails,String newPassword) throws Exception{
		this.getSessionFactory().getCurrentSession().beginTransaction();
		int numOfUpdate = this.getSessionFactory().getCurrentSession().createQuery(updateQuery).
		setString("newPassword", newPassword).
		setInteger("version", userDetails.getVersion()+1).
		setString("updatedAuthor", userDetails.getUsername()).
		setDate("updatedDate", new Date()).setString("userId", userDetails.getUsername()).executeUpdate();
		this.getSessionFactory().getCurrentSession().getTransaction().commit();
		logger.debug("inside authenticatoin dao: method(updatePasswordForUser):  num of rows updated are: " + numOfUpdate);
	}

	public List getUsers(DetachedCriteria detachedCriteria) {
		try{
			return getHibernateTemplate().findByCriteria(detachedCriteria);
		}
		catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}
	
	public boolean updateuser(UserDetailsDTO detailsDTO) {
		
		logger.debug("inside authentication dao: method(updateUser): updating details for user: " + detailsDTO.getUsername());
		
		boolean isSuccess = false;
		try{
			getHibernateTemplate().update(detailsDTO);
			isSuccess = true;
		}
		catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return isSuccess;
		
	}
	
	public void updateUserDetails(UserDetailsDTO userDetailsDTO) throws Exception{
		logger.debug("inside authentication dao: method(updateUserDetails): updating details for user: " + userDetailsDTO.getUsername());
		getHibernateTemplate().saveOrUpdate(userDetailsDTO);
	}
	
	public List<ApplicationUrlMappingDTO> loadUrlMappingList (DetachedCriteria detachedCriteria){
		List<ApplicationUrlMappingDTO> urlMappingDTOList = null;
		try{
			urlMappingDTOList = getHibernateTemplate().findByCriteria(detachedCriteria);
		}
		catch(Exception e){
			logger.error(e.getMessage(), e);
			if(urlMappingDTOList == null) {
				urlMappingDTOList = new ArrayList<ApplicationUrlMappingDTO>();
			}
		}
		return urlMappingDTOList;
	}
	
	public boolean saveNewUser(UserDetailsDTO detailsDTO) {
		try {
			getHibernateTemplate().save(detailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}

	}
	
	public List<RoleDetailsDTO> loadCompleteRolesList() throws Exception{
		return getHibernateTemplate().loadAll(RoleDetailsDTO.class);
	}
	
	public List loadAllBySearchCriteria(DetachedCriteria detachedCriteria) {
		List list = null;
		try{
			list = getHibernateTemplate().findByCriteria(detachedCriteria);
		}
		catch(Exception e){
			logger.error(e.getMessage(),e);
			list = new ArrayList();
		}
		return list;
	}
	
	public boolean deletePreviousRolesOfUser(UserRoleMappingDTO mappingDTO) {
		boolean resultOfDelete = false;
		try {
			getHibernateTemplate().delete(mappingDTO);
			resultOfDelete = true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return resultOfDelete;
	}
	
	public boolean delete(Object object){
		boolean resultOfDelete = false;
		try{
			getHibernateTemplate().delete(object);
			resultOfDelete = true;
		}
		catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return resultOfDelete;
	}
	
	public boolean updateUserRoleMappingDetails(UserRoleMappingDTO mappingDTO) {
		try {
			getHibernateTemplate().saveOrUpdate(mappingDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}
	
	public List loadAll(Class entityClass){
		List results;
			try {
				results = getHibernateTemplate().loadAll(entityClass);
				return results;
			} catch (Throwable e) {
				logger.error(e.getMessage(),e);
			}
		return null;
	}

	public List<UserDetailsDTO> loadAllUsers() throws Exception {
		return getHibernateTemplate().loadAll(UserDetailsDTO.class);
	}
	
	public boolean saveEditedUSerDetails(UserDetailsDTO userDetailsDTO) {

		try {
			getHibernateTemplate().update(userDetailsDTO);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}
	
	public List<String> loadUserForRoleWF(DetachedCriteria detachedCriteria) throws Exception {
		return getHibernateTemplate().findByCriteria(detachedCriteria);
	}
	
	public List<UserRoleMappingDTO> usersListForRoleId(
			DetachedCriteria detachedCriteria) {
		try {
			return getHibernateTemplate().findByCriteria(detachedCriteria);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
}
